package com.leyou.upload.test;

import ch.qos.logback.core.util.TimeUtil;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class WindowTicketTask2 implements Runnable {
    // 属性
    private volatile int count = 0;

    // 定有一个 Lock 接口的实现类对象
    private Lock lock = new ReentrantLock();

    @Override
    public void run() {

        // 循环买票
        for (int i = 0; i < 2; i++) {
            // t1	t2
            // 获取锁
            lock.lock();
            try {

                count++;
                if (count > 10) {
                    break;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+count + (count != 10 ? "," : ""));
            // 释放锁
            lock.unlock();
        }
    }
}







